﻿using System;
using System.Collections.Generic;

namespace TableTypes
{
    public class AttendanceTableEntry
    {
        public bool Present = false;
        public ClassTime ClassTime = null;
        public ClassName ClassName = null;
        public String Course = null;
        public int Unit = 0;
    }

    public class AttendanceTable : List<AttendanceTableEntry>
    {
        public static AttendanceTable Fetch(WebLayer.LoginToken loginToken)
        {
            WebParser.WebPageCache pageCache = new WebParser.WebPageCache();
            pageCache.LoginToken = loginToken;

            return Fetch(pageCache);
        }

        public static AttendanceTable Fetch(WebParser.WebPageCache pageCache)
        {
            WebParser.ClassesWebPage webPage = pageCache.GetPage<WebParser.ClassesWebPage>();

            AttendanceTable attendanceTable = new AttendanceTable();
            foreach (WebParser.ClassesTableEntry webEntry in webPage.AttendanceTableContent)
            {
                AttendanceTableEntry memEntry = new AttendanceTableEntry();

                memEntry.ClassName = ClassName.ParseClassName(webEntry.Type);
                memEntry.ClassTime = ClassTime.ParseAttendedEntry(webEntry.Date,webEntry.Begin,webEntry.End);
                memEntry.Course = webEntry.Course;

                if (Int32.TryParse(webEntry.Unit, out memEntry.Unit) == false)
                {
                    memEntry.Unit = 0;
                }

                memEntry.Present = webEntry.Presence == "yes";

                attendanceTable.Add(memEntry);
            }

            return attendanceTable;
        }
    
    }
}
